#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>

using namespace std;

/* Test Dynamic Stack */
class DStack{
    private:
        const int STACK_INIT_SIZE=256;
        char* stack;
        size_t top;
        size_t size;

    public:
        DStack(){
            stack = NULL;
            top = 0;
            size = 0;
        }

        void* push(size_t s){
            void* ret;
            assert(s > 0);
            if(s+top >= size){
                if(size == 0)
                    size = STACK_INIT_SIZE;
                while(s+top >= size)
                    size += size>>1;
                stack = (char*)realloc(stack,size);
            }

            ret = stack+top;
            top += s;
            return ret;
        }

        void* pop(size_t s){
            assert(s > 0);
            top -= s;
            return stack+top;
        }

        void put(char ch){
            *(char*)push(sizeof(char)) = ch;
        }

        ~DStack(){
            free(stack);
        }
};

int main(){
    DStack ds;
    const char* str = "abcd";
    const int N = 4;
    for(int i = 0; i < 4; i++)
        ds.put(str[i]);
    char* temp = (char*)ds.pop(N);
    cout<<temp<<endl;
    return 0;
}